package working;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.IOException;

// import Helper.DiskEnum;

/**
 * This program reads a disk file. line by line and print to the console as a
 * demo to load and print the disk data.
 * 
 */

public class DiskFileInput {

	private int read;
	private InputStreamReader stream = null;
	public int getRead() {
		return read;
	}

	public void setRead(int read) {
		this.read = read;
	}

	public InputStreamReader getStream() {
		return stream;
	}

	public void setStream(InputStreamReader stream) {
		this.stream = stream;
	}

	private BufferedReader reader = null;

	public BufferedReader getReader() {
		return reader;
	}

	public void setReader(BufferedReader reader) {
		this.reader = reader;
	}

	public DiskFileInput(Helper.DiskEnum localResource) {
		try {

			loadAsset(localResource);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	enum PrintType{
		BIN,
		OCT,
		HEX,
		INT32
	}
	
	private void printPrimarySpace(int count) {
		if (count == Hardware.Address.systemBase) {
			System.out.print("Hardware.Address.systemBase");
		} else if (count == Hardware.Address.userBase) {
			System.out.print("Hardware.Address.userBase");
		} else if (count == Hardware.Address.deviceBase) {
			System.out.print("Hardware.Address.deviceBase");
		}
	}
	
	
	public void printDisk(PrintType type) {
		try {
			int count = 0;
			
			System.out.println("Binary output start..."); // space it between.
			while ((read = reader.read()) != -1) {				
				switch(type) {
				case BIN:
					System.out.print("Index: " + count++ + ", Binary Value: " + Integer.toBinaryString(read) + "\n");
					printPrimarySpace(count);
					break;
				case OCT:
					System.out.print("Index: " + count++ + ", Octal Value: " + Integer.toOctalString(read) + "\n");
					printPrimarySpace(count);
					break;
				case HEX:
					System.out.print("Index: " + count++ + ", Hex Value: " + Integer.toHexString(read) + "\n");
					printPrimarySpace(count);
					break;
				case INT32:
					String s = Integer.toHexString(read);
					System.out.print("Index: " + count++ + ", INT32 Value: " + Integer.parseInt(s, 16) + "\n");
					printPrimarySpace(count);
					break;
				}				
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * Load disk asset as a BufferedReader for use locally.
	 * 
	 * @param localResource
	 * @throws IOException
	 */
	public void loadAsset(Helper.DiskEnum localResource) throws IOException {

		/**
		 * Load the local disk into a stream to be used by the buffer reader.
		 */
		stream = new InputStreamReader(Thread.currentThread()
				.getContextClassLoader()
				.getResourceAsStream(localResource.toString()));
		
		/**
		 * Set the buffer reader using the local input stream.
		 */
		reader = new BufferedReader(stream);		

	}

	public static void main(String[] args) {
		try {

			DiskFileInput in = new DiskFileInput(Helper.DiskEnum.PA1_SOLO_DSK);
			in.printDisk(PrintType.INT32);			
			in.getReader().close(); // close the reader when done.
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

